from fastapi.encoders import jsonable_encoder
from sqlalchemy.orm import Session
from typing import *
from pydantic import BaseModel
from base_db.db_class import Base


ModelType = TypeVar("ModelType", bound=Base)
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)


class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
    def __init__(self, model: Type[ModelType]):
        self.model = model

    def get(self, db: Session, mode_id: Any = None) -> Optional[ModelType]:
        if mode_id:
            return db.query(self.model).filter(self.model.id == mode_id).first()
        else:
            return db.query(self.model).filter()
